package org.example;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 实时薪资计算器的核心逻辑类
 */
public class SalaryCalculator {
    static final Logger logger = LoggerFactory.getLogger(SalaryCalculator.class);
    private double salary; // 薪资（日薪或月薪）
    private int workHoursPerDay; // 每天工作小时数
    private boolean isMonthly; // 是否是月薪
    private long workStartTime; // 开始工作的时间戳
    private double monthlyWorkDays; // 月工作天数配置值

    private long totalWorkTimeToday; // 今天的总工作时间（毫秒）
    private boolean isWorking; // 是否正在工作
    private boolean isPause;

    private double monthlyEarnings; // 本月累计收入（持久化存储值）

    // 每次暂定时，记录一次当前的实时日薪
    private double currentDailyEarnings;

    public SalaryCalculator() {
        reset();
    }

    /**
     * 重置所有计算数据
     */
    public void reset() {
        workStartTime = 0;
        totalWorkTimeToday = 0;
        // 本月总收入
        isWorking = false;
    }

    /**
     * 设置薪资信息
     *
     * @param salary          薪资金额
     * @param isMonthly       是否为月薪
     * @param workHoursPerDay 每天工作小时数
     * @param monthlyWorkDays 月工作天数
     */
    public void setSalaryInfo(double salary, boolean isMonthly, int workHoursPerDay, double monthlyWorkDays) {
        this.salary = salary;
        this.isMonthly = isMonthly;
        this.workHoursPerDay = workHoursPerDay;
        // 月工作天数设置
        this.monthlyWorkDays = monthlyWorkDays;
    }

    public void startWork() {
        if (!isWorking) {
            isPause = false;
            workStartTime = System.currentTimeMillis();
            isWorking = true;
            logger.info("start work >>>");
        }
    }

    public void pauseWork() {
        if (isWorking) {
            isPause = true;
            long currentTime = System.currentTimeMillis();
            totalWorkTimeToday += (currentTime - workStartTime);
            isWorking = false;
            todayFinalEarnings = calculateCurrentEarnings();
            logger.info("pause work >>> curEarnings：" + todayFinalEarnings);
        }
    }

    public void endWork() {
        if (isWorking) {
            pauseWork();
        }
        isPause = false;
        todayFinalEarnings = Math.round(calculateCurrentEarnings() * 100.0) / 100.0;
        monthlyEarnings = Math.round(monthlyEarnings * 100.0) / 100.0 + todayFinalEarnings;
        workStartTime = 0;
        logger.info("stop work >>> finalEarnings：" + todayFinalEarnings + "，monthlyEarnings：" + monthlyEarnings);
    }

    /**
     * 计算当前实时收入
     *
     * @return 当前收入
     */
    public double calculateCurrentEarnings() {
        // 计算总的工作时间（毫秒）
        long totalTime = totalWorkTimeToday;
        if (isWorking) {
            totalTime += (System.currentTimeMillis() - workStartTime);
        }

        // 转换为小时
        double workedHours = totalTime / (1000.0 * 60 * 60);

        // 计算每小时收入
        double hourlyRate;
        if (isMonthly) {
            // 修改为使用配置的月工作天数计算
            hourlyRate = salary / (monthlyWorkDays * workHoursPerDay);
        } else {
            hourlyRate = salary / workHoursPerDay;
        }

        // 返回当前收入（四舍五入到两位小数）
        double result = hourlyRate * workedHours;
        return Math.round(result * 100.0) / 100.0;
    }

    /**
     * 获取每小时收入
     *
     * @return 每小时收入
     */
    public double getHourlyRate() {
        if (isMonthly) {
            return salary / (monthlyWorkDays * workHoursPerDay);
        } else {
            return salary / workHoursPerDay;
        }
    }

    /**
     * 获取本月总收入
     */
    public double getCurrentMonthlyEarnings() {
        if (isWorking) {
            // 如果正在工作，则返回已保存的月收入加上今天的实时收入
            return monthlyEarnings + calculateCurrentEarnings();
        } else {
            // 如果不工作，则返回已保存的月收入
            return monthlyEarnings;
        }
    }

    /**
     * 获取本月总收入
     */
    public double getMonthlyEarnings() {
        // 仅返回字段存储的累计值，不再重复计算当前收入
        return monthlyEarnings;
    }

    /**
     * 获取今日总工作时间（毫秒）
     */
    public long getTotalWorkTimeToday() {
        return totalWorkTimeToday;
    }

    /**
     * 检查是否正在工作
     */
    public boolean isWorking() {
        return isWorking;
    }

    public boolean isPause() {
        return isPause;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    // 修改set方法名称更准确
    public void setInitialMonthlyEarnings(double earnings) {
        this.monthlyEarnings = earnings;
    }

    // 存储当日最终收入
    private double todayFinalEarnings;

    // 获取当日最终收入
    public double getTodayFinalEarnings() {
        return todayFinalEarnings;
    }

    /**
     * 获取当前日薪
     */
    public double getCurrentDailyEarnings() {
        return currentDailyEarnings;
    }

    /**
     * 设置当前日薪
     */
    public void setCurrentDailyEarnings(double dailyEarnings) {
        this.currentDailyEarnings = dailyEarnings;
        logger.info("set current daily earnings >>> " + dailyEarnings);
    }
}
